package com.hcb.tonguetiphealth.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class StringUtils {
	private static DecimalFormat decimalFormat = new DecimalFormat("0.00");
	private static StringBuilder pattern = new StringBuilder();
	/**
	 * 
	 * @param value
	 * @param pionts 小数点后保留位数
	 * @return
	 */
	public static String numFormat(String value, int pionts) {
		pattern.setLength(0);
		pattern.append("0.");
		for(int i=0; i<pionts; i++) {
			pattern.append("0");
		}
		decimalFormat.applyPattern(pattern.toString());
		try {
			return (decimalFormat.format(value));
		} catch(Exception e) {
			return (decimalFormat.format(0));
		}
	}
	
	public static String getDateTime(){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date curDate = new Date(System.currentTimeMillis());
		return formatter.format(curDate);
	}

	public static int parseInt(String input) {
		int i = 0;
		try {
			i = Integer.parseInt(input);
		} catch (Exception e) {
			//
		}
		return i;
	}
	public static long parseLong(String input) {
		long i = 0;
		try {
			i = Long.parseLong(input);
		} catch (Exception e) {
			//
		}
		return i;
	}

	public static float parseFloat(String input) {
		float i = 0;
		try {
			i = Float.parseFloat(input);
		} catch (Exception e) {
			//
		}
		return i;
	}
	
	public static String replaceCharacter(String input) {
		return 	input.replaceAll("&nbsp;", " ").
					  replaceAll("&ldquo;", "\"").
					  replaceAll("&rdquo;", "\"").
					  replaceAll("(\r?\n(\\s*\r?\n)+)", "\n").//替换空格和多个联系的换行成一行
					  replaceAll("&hellip;", "...");
	}

	public static boolean isEmpty(String str) {
		return str == null || str.trim().length() <= 0;
	}
	
	public static String getDigitString(String str) {
		String result = null;
		String regex = "\\d*";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(str);
		while (m.find()) {
			if (!"".equals(m.group())) {
				result = m.group();
			}
		}
		return result;
	}

	public static boolean isIPV4(String input) {
		// InetAddress ia = new InetAddress(input);
		String exp = "\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b";
		return input.matches(exp);
	}

	public static String convertStream(InputStream is) {

		BufferedReader reader = new BufferedReader(new InputStreamReader(is));

		StringBuilder sb = new StringBuilder();
		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return sb.toString();
	}

	private static String TruncateUrl(String strURL) {
		String strAllParam = null;
		String[] arrSplit = null;
		strURL = strURL.trim().toLowerCase();
		arrSplit = strURL.split("[?]");
		if (strURL.length() > 1) {
			if (arrSplit.length > 1) {
				if (arrSplit[1] != null) {
					strAllParam = arrSplit[1];
				}
			}
		}

		return strAllParam;
	}

	public static Map<String, String> parseURL(String URL) {
		Map<String, String> result = new HashMap<String, String>();
		String[] arrSplit = null;
		String strUrlParam = TruncateUrl(URL);
		if (strUrlParam == null) {
			return result;
		}
		// ÿ����ֵΪһ��
		arrSplit = strUrlParam.split("[&]");
		for (String strSplit : arrSplit) {
			String[] arrSplitEqual = null;
			arrSplitEqual = strSplit.split("[=]");

			// ��������ֵ
			if (arrSplitEqual.length > 1) {
				// ��ȷ����
				result.put(arrSplitEqual[0], arrSplitEqual[1]);

			} else {
				if (arrSplitEqual[0] != "") {
					// ֻ�в���û��ֵ��������
					result.put(arrSplitEqual[0], "");
				}
			}
		}
		return result;
	}
	
	public static Map<String, String> pingResult(String pingResult) {

		Map<String, String> map = null;
		List<String> list = null;
		int n = 0;
		int m = 0;
		String line = "";
		String byteNo = "";
		String ttl = "";
		String time = "";
		String count = "";
		String successCount = "";
		
		try {
			if (isEmpty(pingResult)) {
				return map;
			}
			list = split(pingResult, PING_RESULT.PING_RESULT_SEPARATOR_CRC);
			if (list == null) {
				return map;
			}
			line = (String) list.get(1);
			if (isEmpty(line)) {
				return map;
			}
			n = line.indexOf(PING_RESULT.PING_RESULT_BYTES_FROM);
			if (n > 0) {
				byteNo = line.substring(0, n - 1);
			}
			n = line.indexOf(PING_RESULT.PING_RESULT_TTL);
			m = line.indexOf(PING_RESULT.PING_RESULT_TIME);
			if (n > 0 && m > 0) {
				ttl = line.substring(n + 4, m - 1);
			}
			line = (String) list.get(list.size() - 1);
			if (isEmpty(line)) {
				return map;
			}
			n = line.indexOf(PING_RESULT.PING_RESULT_MDEV);
			if (n > 0) {
				String times = line.substring(n + 7);
				List<String> timeList = split(times,
						PING_RESULT.PING_RESULT_SEPARATOR_BSLASH);
				time = (String) timeList.get(1);
			}
			
			
			line = (String) list.get(list.size() - 2);
			if (isEmpty(line)) {
				return map;
			}
			n = line.indexOf(PING_RESULT.PING_PACKETS_TRANSMITTED);
			if (n > 0) {
				count = line.substring(0, n-1);
				System.out.println("PING�ܴ���" + count);
			}
			m = line.indexOf(PING_RESULT.PING_RECEIVED);
			if (n > 0) {
				successCount = line.substring(n+21, m-1);
				System.out.println("PING�ɹ�����" + successCount);
			}
			
			map = new HashMap<String, String>();
			map.put(PING_RESULT.PING_RESULT_BYTE_NO, byteNo);
			map.put(PING_RESULT.PING_RESULT_TTL, ttl);
			map.put(PING_RESULT.PING_RESULT_TIME, time);
			map.put(PING_RESULT.PING_PACKETS_TRANSMITTED, count);
			map.put(PING_RESULT.PING_RECEIVED, successCount);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;

	}
	public interface PING_RESULT {
		static final String PING_RESULT_SEPARATOR_CRC = "\n";
		static final String PING_RESULT_SEPARATOR_BSLASH = "/";
		static final String PING_RESULT_BYTES_FROM = "bytes from";
		static final String PING_RESULT_TTL = "ttl";
		static final String PING_RESULT_TIME = "time";
		static final String PING_RESULT_MDEV = "mdev";
		static final String PING_RESULT_BYTE_NO = "byteNo";
		
		static final String PING_PACKETS_TRANSMITTED = "packets transmitted,";
		static final String PING_RECEIVED = "received,";
	}
	
	public static List<String> split(String str, String delim) {
		List<String> splitList = null;
		StringTokenizer st = null;

		if (str == null)
			return splitList;

		if (delim != null)
			st = new StringTokenizer(str, delim);
		else
			st = new StringTokenizer(str);

		if (st != null && st.hasMoreTokens()) {
			splitList = new ArrayList<String>();
			while (st.hasMoreTokens())
				splitList.add(st.nextToken());
		}
		return splitList;
	}
 
	
}
